#include "common.h"

#include <assert.h>
#include <string.h>

// 使用 SDL 和 标准 IO
#include <SDL2/SDL.h>
#include <stdio.h>
#include <sys/stat.h>
#include <libgen.h>
#include <stdbool.h>
#include "util/log.h"
#include "common_test.h"
#include "src/version.h"
#include "src/opengl.h"

// 参考下面的链接
// https://www.avdancedu.com/a6aca2fe/

// 定义屏幕尺寸常量
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
#define POINTS_COUNT 4

//static SDL_Rect bigrect = {100, 100, 540, 380};

static SDL_Point points[POINTS_COUNT] = {
    // 用下面的顺序画出来就是一个矩形，应该是逆时针顺序就可以
    {0, 0},
    {0, 10},
    {30, 10},
    {30, 0},
    // 下面这个点是不必要的
    {0, 0}
    };

static void render(SDL_Window *window, SDL_Renderer *renderer)
{
    /* We must call SDL_CreateRenderer in order for draw calls to affect this window. */
    renderer = SDL_CreateRenderer(window, -1, 0);

    /* 
    Select the color for drawing. It is set to red here. 
    设置画笔颜色
    最后一个参数 a 指明了颜色的透明度
    */
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

    /* Clear the entire screen to our selected color. */
    SDL_RenderClear(renderer);

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);

    // 画一条线
    SDL_RenderDrawLine(renderer, 30, 10, 20, 60);
    SDL_RenderDrawLines(renderer, points, POINTS_COUNT);

    SDL_Rect rect = {20, 60, 80, 90};
    // 画矩形
    SDL_RenderDrawRect(renderer, &rect);

    SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
    SDL_RenderFillRect(renderer, &rect);

    // 前半部分是坐标，后半部分是大小
    SDL_Rect bigrect = {110, 160, 80, 90};
    SDL_SetRenderDrawColor(renderer, 0, 0, 32, 255);
    SDL_RenderFillRect(renderer, &bigrect);

    /* Up until now everything was drawn behind the scenes.
       This will show the new, red contents of the window. */
    SDL_RenderPresent(renderer);
}

static void print_opengl_version(void){
    // SDL_INIT_EVERYTHING
    struct sc_opengl gl;
    sc_opengl_init(&gl);
    printf("openGL version:%s\n",gl.version);
}

static void test_create_window(void)
{
    // 将要渲染的窗口
    SDL_Window *window = NULL;

    // 一个渲染器
    SDL_Renderer *renderer = NULL;

    SDL_Texture *texture = NULL; // 一个纹理
    SDL_Event event; // 一个事件

    // 窗口含有的surface
    SDL_Surface *screenSurface = NULL;
    SDL_Surface *imageSurface = NULL;

    // 获取到的窗口的宽和高，不是指预设的
    int width, height;
    int ret;

    TEST_START_INFO;
    printf("此测试会创建一个临时的窗口，大约停留两秒钟");

    // 初始化SDL
    ret = SDL_Init(SDL_INIT_VIDEO);
    assert(!ret);

    if (ret < 0)
    {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        return;
    }

    atexit(SDL_Quit); // 退出
    printf("SDL initialize complete!\n");

    // 创建 window，标题是可以直接使用中文的
    window = SDL_CreateWindow("SDL测试窗口名称",
                              SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                              SCREEN_WIDTH, SCREEN_HEIGHT,
                              SDL_WINDOW_SHOWN);

    assert(window);
    if (window == NULL)
    {
        printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
        return;
    }

    // 获取 window surface
    screenSurface = SDL_GetWindowSurface(window);

    // 用指定的颜色填充surface
    SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0x1B, 0xFF, 0xFF));

    /*
        // 加载图片
        imageSurface = SDL_LoadBMP('');
        assert(imageSurface);
        if( imageSurface == NULL )
        {
            printf( "Unable to load image inc! SDL Error: %s\n", SDL_GetError());
            return ;
        }

        SDL_BlitSurface( imageSurface, NULL, screenSurface, NULL );
    */

    // 更新surface,不用渲染器的时候也可以用下面这句显示出窗体
    SDL_UpdateWindowSurface(window);

    render(window, renderer);

    // 延迟1.5秒自动关闭
    SDL_Delay(1500);
    
    /*
    // 等待关闭窗口事件
    int quit=0;
    while(!quit){
        SDL_Event event;
        // SDL_WaitEvent SDL_PollEvent
        // 两个用法不同，对CPU占比也不同，适用于不同的地方
        while(SDL_WaitEvent(&event)){
            switch(event.type){
                case SDL_QUIT:
                    quit = 1;
                    printf("jack test exit \n");
                    break;
                default:
                    printf("%d.\n",event.type);
            }
        }
    }
    */

    // 获取窗体的尺寸并验证
    SDL_GetWindowSize(window, &width, &height);

    printf("width= %d\n", width);
    assert(SCREEN_WIDTH == width);
    printf("height= %d\n", height);
    assert(SCREEN_HEIGHT == height);

    print_opengl_version();

    // 下面是回收资源
    if (renderer)
    {
        // 释放渲染上下文相关的资源。
        SDL_DestroyRenderer(renderer);
    }

    // 销毁 window
    SDL_DestroyWindow(window);

    // 退出 SDL subsystems
    SDL_Quit();

    TEST_END_INFO;
}

static char * get_base_dir(char * dir){
    char buf[80];
    getcwd(buf,sizeof(buf));
    printf("current working directory: %s\n", buf);

    char *dirc, *basec, *bname, *dname;

    dirc = strdup(buf);
    basec = strdup(buf);

    dname = dirname(dirc);

    bname = basename(basec);

    printf("dirname=%s, basename=%s\n", dname, bname);
    return strdup(dname);
}

int main(int argc, char *argv[])
{
    (void)argc;
    (void)argv;

    printf("这是jack添加的测试SDL2的方法\n");
    test_create_window();
    // test_circle();

    /* TODO 通过当前的路径，可以找到对应的图标路径，并把其设置到窗体上吗 */

    char dirc[80];
    get_base_dir(&dirc);

    return 0;
}
